package BFS;

import java.util.LinkedList;
import java.util.Queue;

/**
 * Created with Intellij IDEA.
 * Description;
 * User:TQ02
 * Data:2024-03-12
 * Time:17:16
 * Text：1020. 飞地的数量
 */
public class Texts6 {
    int m,n;
    int[] dx={0,0,1,-1};
    int[] dy={1,-1,0,0};
    public void bfs(Queue<int[]> q,int[][] mat){
        while(!q.isEmpty()){
            int[] array=q.poll();

            for(int i=0;i<4;i++){
                int x=array[0]+dx[i],y=array[1]+dy[i];
                if(x>=0 && x<m && y>=0 && y<n && mat[x][y]==1){
                    mat[x][y]=2;
                    q.add(new int[]{x,y});
                }
            }

        }
    }
    public int numEnclaves(int[][] mat) {
        Queue<int[]> q=new LinkedList<>();
        m = mat.length;
        n = mat[0].length;
        for(int i=0;i< mat.length; i++){
            for(int j = 0; j < n; j++){
                if(mat[i][j]==1 && (i==0 || i==m-1 || j==0 || j==n-1)){
                    mat[i][j]=2;
                    q.add(new int[]{i,j});
                }
            }
        }
        bfs(q,mat);
        int ret=0;
        for(int i=0;i< mat.length; i++){
            for(int j = 0; j < n; j++){
                if(mat[i][j]==1){
                    ret++;
                }
            }
        }
        return  ret;
    }

    public static void main(String[] args) {
        int[][] array={{0,0,0,0},{1,0,1,0},{0,1,1,0},{0,0,0,0}};
        Texts6 t1=new Texts6();
        System.out.println(t1.numEnclaves(array));

    }
}
